"""
组织机构逻辑处理模块

"""
from flask import g, session, request
from flask_login import current_user
from playhouse.shortcuts import model_to_dict

from models.user import Organization
from components.base_log import log_method_call


@log_method_call(action='Retrieve')
def get_org_info():
    data = {"items": []}
    orgs = Organization.select().where(Organization.id << g.real_org_ids)
    for org in orgs:
        data['items'].append(model_to_dict(org))
    return data


@log_method_call(action='Update')
def update_org_info():
    org = Organization.get_or_none(Organization.id == g.json.get('id'))
    org.name = g.json.get('name')
    org.company_type = g.json.get('company_type')
    org.address = g.json.get('address')
    org.phone = g.json.get('phone')
    org.save()
    return model_to_dict(org)


@log_method_call(action='Update')
def update_logo_for_org(logo_path):
    org = Organization.get_or_none(Organization.id == request.args.get('org_id'))
    org.logo = logo_path
    org.save()
